#!/bin/bash
#
# Copyright 2011 Google Inc. All Rights Reserved.
# Author: sdd@google.com (Salvatore Domenick Desiano)
#
# Copies changes from the edit clone to the issue clone and uploads
# them as an additional patch set to an existing issue on a Rietveld
# server. Only works properly if executed as part of a GLP
# checkout. See glp-common.sh for more information about the
# development process used by GLP developers.
#
# usage: src/scripts/glp-issue-update


# Read parameters, initialize variables, and authenticate user.
source "$(dirname "${0}")/glp-common.sh"
glp::initialize_checkout_variables
if [[ "${ISSUE_STATE}" != 'active' ]]; then
  glp::die 'issue state must be active'
fi
"${SCRIPT_DIR}/glp-issue-cache-password" "${RIETVELD_UPLOAD_SCRIPT}" \
    ${RIETVELD_PARAMS} || glp::die 'unable to cache Rietveld credentials'
echo "Updating issue ${ISSUE_NUMBER}..."


# Update the review clones from the upstream repository and edit
# clone.
glp::update_review_clones 'new_issue_rev' 'source: glp-issue-update'
if [[ "${new_issue_rev}" == "${ISSUE_REV}" ]]; then
    glp::die "no changes detected since last update."
fi

    
# Upload patch set to Rietveld.
echo "Uploading patch set to ${RIETVELD_URL}..."
cd "${ISSUE_CLONE}" || glp::die 'failure.'
"${RIETVELD_UPLOAD_SCRIPT}" ${RIETVELD_PARAMS} --issue=${ISSUE_NUMBER} \
    "--rev=${ISSUE_BASE_REV}" \
    "--message=Update from ${ORG_USER} on $(date)" || \
    glp::die 'failure.'
cd - > /dev/null || glp::die 'failure'


# Update review state.
echo "  updating issue state..."
glp::write_issue_state 'active' ${ISSUE_NUMBER} "${ISSUE_BASE_REV}" \
    "${new_issue_rev}"

echo "Issue ${ISSUE_NUMBER} updated."
